# encoding=utf-8

import os
import re
import pandas as pd
import yaml  # pip install pyyaml
from win32com import client as wc  # python -m pip install pypiwin32

from ner_predict import predict_online
from z_read.to_kp_file import generate_text


def list_all_files(rootdir):
    """
    列出文件夹下所有的目录与文件
    :param rootdir: 根路径
    :return:
    """
    _files = []
    list = os.listdir(rootdir)  # 列出文件夹下所有的目录与文件
    for i in range(0, len(list)):
        path = os.path.join(rootdir, list[i])
        if os.path.isdir(path):
            _files.extend(list_all_files(path))
        if os.path.isfile(path):
            _files.append(path)
    return _files


def read_ymal(section, optin):
    """
    打开yaml文件，返回内容
    :param section: 第一级
    :param optin: 第二级
    :return:
    """
    with open("config.yaml", encoding='utf-8') as file:
        # 加载yaml数据
        data_dicts = yaml.load(file)  # 返回是多层字典
        # 从字典中获取数据
        data = data_dicts[section][optin]
        # data原本是什么类型的数据，就返回什么类型的数据
        return data


def doc2docx(path):
    """
    将path路径下的doc文件转换为docx文件
    :param path:
    :return: 返回文件列表
    """
    files = list_all_files(path)
    # 先将文件中的doc转换为docx，并删除非docx文件
    word = wc.Dispatch("Word.Application")  # 打开word应用程序
    for file in files:
        if (file.endswith('.doc')):  # 将doc文件转换为docx文件
            doc = word.Documents.Open(file)  # 打开word文件
            doc.SaveAs("{}x".format(file), 12)  # 另存为后缀为".docx"的文件，其中参数12指docx文件
            doc.Close()  # 关闭原来word文件
    word.Quit()
    return list_all_files(path)


def output_point():
    inputPath = read_ymal("extract", "inputPath")
    dictOutputPath = inputPath + read_ymal("extract", "dictOutputPath")
    delText = read_ymal("extract", "delText")
    if not os.path.exists(dictOutputPath):
        os.makedirs(dictOutputPath)
    # 将path路径下的doc文件转换为docx文件
    files = doc2docx(inputPath)
    # 提出docx中的句子
    for file in files:
        if (file.endswith(".docx")):
            (filepath, filename) = os.path.split(file)
            generate_text(filepath, filename, dictOutputPath, delText)
        else:
            continue
    # 提取知识点
    all_csv = list_all_files(dictOutputPath)
    books = []
    text = []
    knowledge_point = []
    for csv in all_csv:
        # 路径中有中文 python版本较低 无法直接read_csv
        names = pd.read_csv(open(csv, encoding='utf-8'), usecols=['name'])
        for name in names.itertuples():
            sentence = getattr(name, 'name')
            print("文本：", sentence)
            if isinstance(sentence, str):
                point = predict_online(sentence)
                point = re.sub('#', '', point)
                print("知识点：", point)
            (filepath, filename) = os.path.split(csv)
            books.append(re.sub('.csv', '', filename))
            text.append(sentence)
            knowledge_point.append(point)
    result = {'book': books, 'text': text, 'knowledge point': knowledge_point}
    df = pd.DataFrame(result)
    pointOutputPath = inputPath + read_ymal("extract", "pointOutputPath")
    if not os.path.exists(pointOutputPath):
        os.makedirs(pointOutputPath)
    df.to_csv(pointOutputPath + 'result.csv')


if __name__ == "__main__":
    output_point()

